﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace hoangnam12
{
    public partial class ChitietSP : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            dataprovider da = new dataprovider();
            string id = Request.QueryString["ID"].ToString();
            string str = "select TenSP,GiaSP,MoTaSP,HinhSP from SanPham where (MaSP = '"+id+"')";
            
           SqlDataReader reader = da.ExecuteReader(str);
           while (reader.Read())
           {
               Label1.Text = String.Format("{0}", reader[0]);
               Label2.Text = String.Format("{0}", reader[1]);
               Label3.Text = String.Format("{0}", reader[2]);
               Image1.ImageUrl = String.Format("{0}", reader[3]);
           }

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            
                if (Session["login"] == null)
                    Response.Redirect("DangNhap.aspx");
                else
                {


                    string idSP = Request.QueryString["ID"].ToString();
                    double Dongia = double.Parse(Label2.Text);
                        string TenSP = Label1.Text;
                    
                    
                    int Soluong = 1;
                    DataTable dt;
                    if (Session["GioHang"] == null)
                    {
                        dt = new DataTable();
                        dt.Columns.Add("idSP");
                        dt.Columns.Add("TenSP");
                        dt.Columns.Add("Gia");
                        dt.Columns.Add("SoLuong");
                        dt.Columns.Add("TongTien");
                    }
                    else
                        dt = (DataTable)Session["GioHang"];
                    int dong = -1;
                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        if (dt.Rows[i]["idSP"].ToString() == idSP)
                        {
                            dong = i;
                            break;
                        }
                    }
                    if (dong != -1)
                    {
                        dt.Rows[dong]["SoLuong"] = Convert.ToInt32(dt.Rows[dong]["SoLuong"]) + Soluong;
                    }
                    else
                    {
                        DataRow dr = dt.NewRow();
                        dr["idSP"] = idSP;
                        dr["TenSP"] = TenSP;
                        dr["Gia"] = Dongia;
                        dr["SoLuong"] = Soluong;
                        dr["TongTien"] = Dongia * Soluong;
                        dt.Rows.Add(dr);
                    }
                    Session["GioHang"] = dt;
                    Response.Write("<script>alert('Đã thêm vào giỏ hàng ^.*!...')</script>");

                }
        }
    }
}